package com.unisal.java;

import java.util.Observable;
import java.util.Observer;

public class MostraEstatisticasWithObservable implements MostraElementos, Observer {

	private Double maxTemp = 0.0;
	private Double minTemp = 200.0;
	private Double tempSum = 0.0;
	private Integer quantidadeDeLeituras = 0;
	@SuppressWarnings("unused")
	private Observable observable;

	public MostraEstatisticasWithObservable(Observable observable) {
		this.observable = observable;
		observable.addObserver(this);
	}

	@Override
	public void mostra() {
		System.out.println("Media/Max/Min temperature = "
				+ (tempSum / quantidadeDeLeituras) + "/" + maxTemp + "/" + minTemp);
	}

	@Override
	public void update(Observable observable, Object arg) {
		WeatherDataWithObservable weatherData = (WeatherDataWithObservable)observable;
		Double temp = weatherData.getTemperatura();

		tempSum += temp;
		quantidadeDeLeituras++;

		if (temp > maxTemp) {
			maxTemp = temp;
		}

		if (temp < minTemp) {
			minTemp = temp;
		}

		mostra();
		
	}
}